<template>
  <div>
    <div class="com-box">
      <el-row>
        <el-col :span="14">
          <el-form :model="form" ref="formRef" label-width="80px" label-position="left">
            <el-form-item label="评分" prop="score">
              <el-rate v-model="form.star" size="large" />
            </el-form-item>
            <el-form-item label="图片/视频">
              <m-upload v-model="form.resource" />
            </el-form-item>
            <el-row>
              <el-col :span="20">
                <el-form-item label="内容">
                  <el-input
                    v-model="form.content"
                    type="textarea"
                    placeholder="请输入评论"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item>
              <el-button color="#1aa684" @click="addCommentForm">提交</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="10">
          <div class="order-item">
            <div class="order-attr">
              <div class="item-title">{{ product.skuName }}</div>
              <div class="attr-list">
                <div
                  class="attr"
                  v-for="item in formatJSONToObj(product.skuAttrsVals)"
                  :key="item.attrId"
                  v-if="product.skuAttrsVals"
                >
                  {{ item.attrName }}：{{ item.attrValue }}；
                </div>
              </div>
              <image-preview
                :width="300"
                :height="300"
                :src="product.skuPic"
              ></image-preview>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script setup>
import { useRoute, useRouter } from "vue-router";
import mUpload from "@/components-mall/MUpload";
import useUserStore from "@/store/modules/user";
import { postOrderComment } from "@/api/mall/client/order/comment";
import { getOrderItemInfo } from "@/api/mall/client/order/order";
import { getCurrentInstance } from "vue";

const { proxy } = getCurrentInstance();
const userStore = useUserStore();
const formRef = ref();
const route = useRoute();
const router = useRouter();
const query = route.query;
const form = ref({
  skuId: query.skuId,
  spuId: query.spuId,
  memberNickName: userStore.name,
  memberIcon: userStore.avatar,
  orderItemId: query.orderItemId,
});
const addCommentForm = async () => {
  console.log(form.value);
  form.value.resources = JSON.stringify(form.value.resource);
  const res = await postOrderComment(form.value);
  proxy.$modal.msgSuccess("评论成功");
  router.push({
    path: "/user",
  });
};
// 获取商品订单详情
const product = ref([]);
const getProductInfo = async () => {
  const res = await getOrderItemInfo(query.orderItemId);
  product.value = res.data;
};
getProductInfo();
// json
const formatJSONToObj = (item) => {
  if (item) {
    return JSON.parse(item);
  } else {
    return [];
  }
};
</script>

<style lang="scss" scoped>
.com-box {
  background: #fff;
  padding: 20px;
  .order-item {
    display: flex;
    flex-direction: column;
    // align-items: flex-end;
    align-items: center;
    .order-attr {
      margin-left: 10px;
      .item-title {
        font-size: 18px;
        line-height: 30px;
      }
      .attr-list {
        display: flex;
        flex-wrap: wrap;
        margin-bottom: 10px;
      }

      .attr {
        color: #999;
      }
    }
  }
}

:deep(.el-upload--picture-card:hover) {
  border-color: #45b584;
  color: #45b584;
}

.el-upload:focus {
  border-color: #45b584;
  color: #45b584;
}
:deep(.el-textarea__inner:focus) {
  border-color: #45b584;
  box-shadow: 0 0 0 1px #45b584;
}
</style>
